/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.scene.web;

import com.lczyfz.istep.common.config.Global;
import com.lczyfz.istep.common.constants.DelFlag;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.utils.CheckUtils;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.modules.openstack.api.neutron.impl.NetworksImpl;
import com.lczyfz.istep.modules.openstack.api.neutron.impl.RouterImpl;
import com.lczyfz.istep.modules.openstack.api.neutron.model.Network;
import com.lczyfz.istep.modules.scene.entity.*;
import com.lczyfz.istep.modules.scene.service.SceneRouterService;
import com.lczyfz.istep.modules.scene.service.SceneSubnetService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 路由管理Controller
 * @author dh
 * @version 2017-03-20
 */
@Controller
@RequestMapping(value = "${adminPath}/scene/sceneRouter")
public class SceneRouterController extends BaseController {

	@Autowired
	private SceneRouterService sceneRouterService;
	@Autowired
	private SceneSubnetService sceneSubnetService;
	@ModelAttribute
	public SceneRouter get(@RequestParam(required=false) String id) {
		SceneRouter entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sceneRouterService.get(id);
		}
		if (entity == null){
			entity = new SceneRouter();
		}
		return entity;
	}

	@RequiresPermissions("scene:sceneRouter:view")
	@RequestMapping(value = {"list", ""})
	public String list(SceneRouter sceneRouter, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SceneRouter> page = sceneRouterService.findPage(new Page<SceneRouter>(request, response), sceneRouter);
		model.addAttribute("page", page);
		return "modules/scene/sceneRouterList";
	}

	@RequiresPermissions("scene:sceneRouter:view")
	@RequestMapping(value = "form")
	public String form(SceneRouter sceneRouter, Model model,String type) {
		logger.debug("dingh===type:"+type);
		sceneRouter=sceneRouterService.get(sceneRouter);
		if(type!=null&&type.equals("copy"))
		{
			sceneRouter.setRouterName(sceneRouter.getRouterName() + "-副本");
		}
		SceneNetwork sceneNetwork=new SceneNetwork();
		try{
			NetworksImpl networkdemo = new NetworksImpl();
			List<Network> networks = networkdemo.getNetwork();
			logger.debug("dingh===public==provider");

			for(Network network : networks){
				if(network.getName().equals("provider"))
				{
					logger.debug("dingh===public==" + network.getId()+ " " + network.getName());

					sceneNetwork.setId(network.getId());
					sceneNetwork.setNetworkName(network.getName());
					break;
				}
			}

		}catch (RuntimeException e){

		}




		model.addAttribute("sceneNetwork", sceneNetwork);
		model.addAttribute("type", type);
		model.addAttribute("sceneRouter", sceneRouter);
		return "modules/scene/sceneRouterForm";
	}

	@RequiresPermissions("scene:sceneRouter:edit")
	@RequestMapping(value = "save")
	public String save(SceneRouter sceneRouter, Model model, RedirectAttributes redirectAttributes,String type) {
		if (!beanValidator(model, sceneRouter)){
			return form(sceneRouter, model,type);
		}
		logger.debug("新建路由===13" + sceneRouter.getRouterName() + " " + sceneRouter.getId());

		if(type!=null&&type.equals("copy"))
		{
			logger.debug("复制！");
			List<SceneRouter> routers=sceneRouterService.getRouterByName(sceneRouter);

			//新建路由
			if(routers.size()>0)
			{
				addMessage(redirectAttributes, "复制路由'"+sceneRouter.getRouterName()+"'失败，该路由已存在");
			}
			else {
//				try{
//					RouterImpl routerDemo = new RouterImpl();
//					Router router=new Router();
//					router.setRouterName(sceneRouter.getRouterName());
//					router.setRouterNetworkId(sceneRouter.getNetworkId());
//					Router new_router=routerDemo.createRouter(router);
//
//					logger.debug("新建路由id==="+new_router.getRouterId());
//
//					sceneRouter.setId(new_router.getRouterId());
//					sceneRouter.setRouterStatus(new_router.getRouterStatus());
//					sceneRouter.setProjectId(new_router.getRouterProject());
//					sceneRouter.setTenantId(new_router.getRouterTenant());
//
//					logger.debug("新建路由ddd===");
////				String id = IdGen.uuid();
//					logger.debug("NetworkId=== [{}]", sceneRouter.getNetworkId());
////				sceneRouter.setId(id);
//					sceneRouterService.preInsert(sceneRouter);
//					sceneRouterService.insert(sceneRouter);
//
//					logger.debug("新建路由id==="+sceneRouter.getId());
//
//					addMessage(redirectAttributes, "复制路由'"+sceneRouter.getRouterName()+"'成功");
//				}catch (RuntimeException e){
//					addMessage(redirectAttributes, "复制路由'"+sceneRouter.getRouterName()+"'失败");
//
//				}
				sceneRouterService.save(sceneRouter);
				addMessage(redirectAttributes, "复制路由'" + sceneRouter.getRouterName() + "'成功");

			}

		}if(type.equals("setGateWay"))
		{
			logger.debug("设置网关！");
//			try{
//				RouterImpl routerDemo = new RouterImpl();
//				Router router=new Router();
//				router.setRouterName(sceneRouterService.get(sceneRouter.getId()).getRouterName());
//				router.setRouterNetworkId(sceneRouter.getNetworkId());
//				routerDemo.updateRouter(sceneRouter.getId(), router);
//				logger.debug("设置网关！" + router.getRouterNetworkId());
//
//				sceneRouter.setNetworkId(router.getRouterNetworkId());
//				sceneRouterService.save(sceneRouter);
//				addMessage(redirectAttributes, "设置网关成功");
//			}catch (RuntimeException e){
//				addMessage(redirectAttributes, "设置网关失败");
//			}
//			SceneRouter router=sceneRouterService.get(sceneRouter.getId());
//			router.setNetworkId(sceneRouter.getNetworkId());
			sceneRouterService.save(sceneRouter);
			addMessage(redirectAttributes, "设置网关成功");
		}
		else {
			if(sceneRouterService.get(sceneRouter)==null)
			{
				logger.debug("插入！");
				//判断是否已存在该路由
				List<SceneRouter> routers=sceneRouterService.getRouterByName(sceneRouter);
				logger.debug("新建路由==="+sceneRouter.getRouterName()+" "+routers.size());

				//新建路由
				if(routers.size()>0)
				{
					addMessage(redirectAttributes, "添加路由'"+sceneRouter.getRouterName()+"'失败，该路由已存在");

				}
				else {
					sceneRouterService.save(sceneRouter);
					addMessage(redirectAttributes, "添加路由成功");

//					try{
//						RouterImpl routerDemo = new RouterImpl();
//						Router router=new Router();
//						router.setRouterName(sceneRouter.getRouterName());
//						router.setRouterNetworkId(sceneRouter.getNetworkId());
//						Router new_router=routerDemo.createRouter(router);
//						logger.debug("新建路由id==="+new_router.getRouterId());
//
//
//						sceneRouter.setId(new_router.getRouterId());
//						sceneRouter.setRouterStatus(new_router.getRouterStatus());
//						sceneRouter.setProjectId(new_router.getRouterProject());
//						sceneRouter.setTenantId(new_router.getRouterTenant());
//
//						logger.debug("新建路由ddd===");
////				String id = IdGen.uuid();
//						logger.debug("NetworkId=== [{}]", sceneRouter.getNetworkId());
////				sceneRouter.setId(id);
//						sceneRouterService.preInsert(sceneRouter);
//						sceneRouterService.insert(sceneRouter);
//						logger.debug("新建路由id===" + sceneRouter.getId());
//
//						addMessage(redirectAttributes, "添加路由成功");
//					}catch (RuntimeException e){
//						addMessage(redirectAttributes, "添加路由失败");
//
//					}
//

				}
			}else {
				//修改路由
				logger.debug("修改！");
				sceneRouterService.save(sceneRouter);
				addMessage(redirectAttributes, "修改路由成功");
//				try{
//					RouterImpl routerDemo = new RouterImpl();
//					Router router=new Router();
//					router.setRouterName(sceneRouter.getRouterName());
//					router.setRouterNetworkId(sceneRouterService.get(sceneRouter.getId()).getNetworkId());
//					routerDemo.updateRouter(sceneRouter.getId(),router);
//					sceneRouterService.save(sceneRouter);
//					addMessage(redirectAttributes, "修改路由成功");
//				}catch (RuntimeException e){
//					addMessage(redirectAttributes, "修改路由失败");
//				}

			}
		}
		return "redirect:"+Global.getAdminPath()+"/scene/sceneRouter/?repage";
	}

	@RequiresPermissions("scene:sceneRouter:edit")
	@RequestMapping(value = "delete")
	public String delete(SceneRouter sceneRouter, RedirectAttributes redirectAttributes) {
		try{
			RouterImpl routerDemo = new RouterImpl();
			routerDemo.deleteRouter(sceneRouter.getId());

			sceneRouterService.delete(sceneRouter);

			logger.debug("deleteRouterId=== [{}]", sceneRouter.getId());
//		routerDemo.deleteRouter(sceneRouter.getId());
			addMessage(redirectAttributes, "删除路由成功");
		}catch (RuntimeException e){
			addMessage(redirectAttributes, "删除路由失败");

		}


		return "redirect:"+Global.getAdminPath()+"/scene/sceneRouter/?repage";
	}
	@RequiresPermissions("scene:sceneRouter:view")
	@RequestMapping(value = "addInterface")
	public String addInterface(SceneRouter sceneRouter, Model model) {
		SceneSubnet sceneSubnet=new SceneSubnet();
		List<SceneSubnet> sceneSubnetList=sceneSubnetService.findList(sceneSubnet);
		List<SceneSubnet> temp=sceneRouterService.getSubnetList(sceneRouter);
		logger.debug("deleteRouterId=== [{}]", sceneSubnetList.size());

		for(int i=0;i<temp.size();i++)
		{
			for(int j=0;j<sceneSubnetList.size();j++)
			{
				if(temp.get(i).getId().equals(sceneSubnetList.get(j).getId()))
				{
					sceneSubnetList.remove(j);
					break;
				}
			}
		}
		for(int j=0;j<sceneSubnetList.size();j++)
		{
			logger.debug("deleteRouterId=== [{}]", sceneSubnetList.get(j).getId()+"  "+sceneSubnetList.get(j).getSubnetName()+"  "+sceneSubnetList.get(j).getNetworkName());
		}
		logger.debug("deleteRouterId=== [{}]", sceneSubnetList.size());
		logger.debug("deleteRouterId=== [{}]", temp.size());

		model.addAttribute("subnetList", sceneSubnetList);
		model.addAttribute("sceneRouter", sceneRouter);
		return "modules/scene/sceneRouterAddInterface";
	}


	@RequiresPermissions("scene:sceneRouter:edit")
	@RequestMapping(value = "saveInterface")
	public String saveInterface(SceneRouter sceneRouter, RedirectAttributes redirectAttributes,String subnetId) {
		logger.debug("deleteRouterId=== [{}]", sceneRouter.getId() + "  " + subnetId);
//		try{
//			RouterImpl routerDemo = new RouterImpl();
//			routerDemo.addRouterInterface(sceneRouter.getId(),subnetId);
//
//			sceneRouterService.saveInterface(sceneRouter.getId(), subnetId);
//			addMessage(redirectAttributes, "增加接口成功");
//		}catch (RuntimeException e){
//			addMessage(redirectAttributes, "增加接口失败");
//
//		}
		sceneRouterService.saveInterface(sceneRouter.getId(), subnetId);
		addMessage(redirectAttributes, "增加接口成功");


		return "redirect:"+Global.getAdminPath()+"/scene/sceneRouter/?repage";
	}

	@RequiresPermissions("scene:sceneRouter:edit")
	@RequestMapping(value = "removeGateWay")
	public String removeGateWay(SceneRouter sceneRouter, RedirectAttributes redirectAttributes) {
		logger.debug("deleteRouterId===ssss [{}]");


//
//		try{
//			RouterImpl routerDemo = new RouterImpl();
//			Router router=new Router();
//			routerDemo.updateRouter(sceneRouter.getId(),router);
//			sceneRouterService.removeGateWay(sceneRouter);
//			addMessage(redirectAttributes, "清除网关成功");
//		}catch (RuntimeException e){
//			addMessage(redirectAttributes, "清除网关失败");
//		}
		sceneRouter.setNetworkId(null);
		sceneRouterService.save(sceneRouter);
		return "redirect:"+Global.getAdminPath()+"/scene/sceneRouter/?repage";
	}
	/**
	 * 查看路由接口列表
	 */
	@ResponseBody
	@RequestMapping(value = "interfaceList")
	public List<SceneSubnet> interfaceList(@RequestParam(required = false) String ids) {
		SceneSubnet sceneSubnet=new SceneSubnet();
		sceneSubnet.setRouterId(ids);
		return sceneSubnetService.getSubnetListByRouterId(sceneSubnet);
	}
	/**
	 * 查看防火墙规则列表
	 */
	@ResponseBody
	@RequestMapping(value = "findFireRuleList")
	public List<SceneFirewallRule> findFireRuleList(@RequestParam(required = false) String ids) {
		logger.debug("ids=== [{}]", ids);
		List<SceneFirewallRule> firewallRuleList=sceneRouterService.findFirewallRuleList(ids);
		return firewallRuleList;
	}
	@RequiresPermissions("scene:sceneRouter:view")
	@RequestMapping(value ="interfaceManage")
	public String interfaceManage(SceneRouter sceneRouter, HttpServletRequest request, HttpServletResponse response, Model model) {

//		Page<SceneRouter> page = sceneRouterService.findPage(new Page<SceneRouter>(request, response), sceneRouter);
		SceneSubnet sceneSubnet=new SceneSubnet();
		sceneSubnet.setRouterId(sceneRouter.getId());
		Page<SceneSubnet> page = sceneSubnetService.findPageByRouterId(new Page<SceneSubnet>(request, response),sceneSubnet);
		model.addAttribute("page", page);
		model.addAttribute("sceneRouter", sceneRouter);

		logger.debug("page=== [{}]", page.getList().size());

		return "modules/scene/sceneRouterInterfaceList";
	}
	@RequiresPermissions("scene:sceneRouter:view")
	@RequestMapping(value ="deleteInterface")
	public String deleteInterface(String subnetId,String routerId, Model model,  RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response) {
//		try{
//			RouterImpl routerDemo = new RouterImpl();
//			routerDemo.removeRouterInterface(routerId,subnetId);
////		Page<SceneRouter> page = sceneRouterService.findPage(new Page<SceneRouter>(request, response), sceneRouter);
//			sceneRouterService.deleteInterface(subnetId,routerId);
//			SceneSubnet sceneSubnet=new SceneSubnet();
//			SceneRouter sceneRouter=new SceneRouter();
//			sceneRouter.setId(routerId);
//			sceneSubnet.setRouterId(routerId);
//			Page<SceneSubnet> page = sceneSubnetService.findPageByRouterId(new Page<SceneSubnet>(request, response),sceneSubnet);
//			model.addAttribute("page", page);
//			model.addAttribute("sceneRouter", sceneRouter);
//			addMessage(redirectAttributes, "删除接口成功");
//		}catch (RuntimeException e){
//			addMessage(redirectAttributes, "删除接口失败");
//
//		}
		sceneRouterService.deleteInterface(subnetId,routerId);
		SceneSubnet sceneSubnet=new SceneSubnet();
		SceneRouter sceneRouter=new SceneRouter();
		sceneRouter.setId(routerId);
		sceneSubnet.setRouterId(routerId);
		Page<SceneSubnet> page = sceneSubnetService.findPageByRouterId(new Page<SceneSubnet>(request, response),sceneSubnet);
		model.addAttribute("page", page);
		model.addAttribute("sceneRouter", sceneRouter);
		addMessage(redirectAttributes, "删除接口成功");
		return "modules/scene/sceneRouterInterfaceList";

	}
	//路由表
	@RequiresPermissions("scene:sceneRouter:view")
	@RequestMapping(value = "topologyRouterList")
	public String topologyRouterList(String environmentId,Model model, RedirectAttributes redirectAttributes,String type) {
//		environmentId="cc4287a0-4084-11e7-abe6-d8cb8a6cc1b8";
		logger.debug("type==路由列表= [{}]", type);

		SceneEnvironment sceneEnvironment=new SceneEnvironment();
		sceneEnvironment.setId(environmentId);
		List<SceneRouter> routerList=sceneRouterService.findRouterListByEnvironmentId(DelFlag.DEL_FLAG_NORMAL,sceneEnvironment.getId());


		if(routerList.size()>0)
		{
			model.addAttribute("type", type);
			model.addAttribute("environmentId", environmentId);
			model.addAttribute("routerList", routerList);
			return "modules/scene/sceneTopologyRouterList";
		}else {
			List<SceneFirewall> firewallList=sceneRouterService.findFirewallListByEnvironmentId(environmentId);
			if(firewallList.size()>0)
			{
				model.addAttribute("type", type);
				model.addAttribute("firewallList", firewallList);
				model.addAttribute("environmentId", environmentId);
				return "modules/scene/sceneFirewallList";
			}else {
				if(type!=null&&type.equals("ctf"))
				{
					return "redirect:"+Global.getAdminPath()+"/scene/sceneCtfEnvironment/?repage";

				}else
					return "redirect:"+Global.getAdminPath()+"/scene/sceneEnvironment/?repage";

			}

		}

	}
	/**
	 * 查看路由表接口列表
	 */
	@ResponseBody
	@RequestMapping(value = "routerTableList")
	public List<SceneRouterTable> routerTableList(@RequestParam(required = false) String ids) {
		List<SceneRouterTable> routerTableList=sceneRouterService.findRouterTableListByRouterId(ids);
		return routerTableList;
	}
	@RequiresPermissions("scene:sceneRouter:view")
	@RequestMapping(value = "routerTableAdd")
	public String routerTableAdd(String routerId ,String environmentId, Model model,String type) {
		logger.debug("type==添加路由表= [{}]", type);

		SceneSubnet sceneSubnet=new SceneSubnet();
		sceneSubnet.setRouterId(routerId);
		List<SceneSubnet> subnetList=sceneSubnetService.getSubnetListByRouterId(sceneSubnet);
		model.addAttribute("subnetList", subnetList);
		model.addAttribute("type", type);
		model.addAttribute("routerId", routerId);
		model.addAttribute("environmentId", environmentId);
		return "modules/scene/sceneRouterTableForm";
	}
	@RequiresPermissions("scene:sceneRouter:edit")
	@RequestMapping(value = "routerTableSave")
	public String routerTableSave(SceneRouterTable sceneRouterTable,String subnetId,String environmentId, Model model,String type, RedirectAttributes redirectAttributes) {
		logger.debug("dingh=== [{}] [{}] [{}] [{}]", sceneRouterTable.getRouterId(),sceneRouterTable.getDestination(),sceneRouterTable.getNexthop(),environmentId);
		logger.debug("type==保存路由表= [{}]", type);

		if(!CheckUtils.isIp(sceneRouterTable.getNexthop()))
		{
			logger.debug("type==ip错误");

			SceneSubnet sceneSubnet=new SceneSubnet();
			sceneSubnet.setRouterId(sceneRouterTable.getRouterId());
			List<SceneSubnet> subnetList=sceneSubnetService.getSubnetListByRouterId(sceneSubnet);
			model.addAttribute("subnetList", subnetList);
			model.addAttribute("type", type);
			model.addAttribute("routerId", sceneRouterTable.getRouterId());
			model.addAttribute("environmentId", environmentId);
			addMessage(redirectAttributes, "下一跳IP不正确，添加路由表失败");
			return "modules/scene/sceneRouterTableForm";
		}
		try{
			if(!CheckUtils.isIpInRange(sceneRouterTable.getNexthop(),subnetId))
			{
				logger.debug("type==ip不在子网内");


				SceneSubnet sceneSubnet=new SceneSubnet();
				sceneSubnet.setRouterId(sceneRouterTable.getRouterId());
				List<SceneSubnet> subnetList=sceneSubnetService.getSubnetListByRouterId(sceneSubnet);
				model.addAttribute("subnetList", subnetList);
				model.addAttribute("type", type);
				model.addAttribute("routerId", sceneRouterTable.getRouterId());
				model.addAttribute("environmentId", environmentId);
				addMessage(redirectAttributes, "下一跳IP不在子网地址中，添加路由表失败");
				return "modules/scene/sceneRouterTableForm";
			}
		}catch (Exception e)
		{
		}

		sceneRouterService.routerTableSave(sceneRouterTable);


		SceneEnvironment sceneEnvironment=new SceneEnvironment();
		sceneEnvironment.setId(environmentId);
		List<SceneRouter> routerList=sceneRouterService.findRouterListByEnvironmentId(DelFlag.DEL_FLAG_NORMAL,sceneEnvironment.getId());
		model.addAttribute("environmentId", environmentId);
		model.addAttribute("type", type);
		model.addAttribute("routerList", routerList);
		addMessage(redirectAttributes, "添加路由表成功");
		return "modules/scene/sceneTopologyRouterList";
	}

	@RequiresPermissions("scene:sceneRouter:view")
	@RequestMapping(value = "firewallList")
	public String firewallList(String environmentId, Model model,String type) {
		logger.debug("type==防火墙列表= [{}]", type);

		List<SceneFirewall> firewallList=sceneRouterService.findFirewallListByEnvironmentId(environmentId);
		if(firewallList.size()==0)
		{
			if(type.equals("ctf"))
			{
				return "redirect:"+Global.getAdminPath()+"/scene/sceneCtfEnvironment/?repage";

			}else {
				return "redirect:"+Global.getAdminPath()+"/scene/sceneEnvironment/?repage";
			}
		}
		model.addAttribute("type", type);
		model.addAttribute("firewallList", firewallList);
		model.addAttribute("environmentId", environmentId);
		return "modules/scene/sceneFirewallList";
	}
	@RequiresPermissions("scene:sceneRouter:view")
	@RequestMapping(value = "firewallForm")
	public String firewallForm(String firewallId,String environmentId, Model model) {
		logger.debug("dingh==firewallForm=routerId= [{}]", firewallId);

		model.addAttribute("environmentId", environmentId);
		model.addAttribute("firewallId", firewallId);
		return "modules/scene/sceneFirewallForm";
	}
	@RequiresPermissions("scene:sceneRouter:edit")
	@RequestMapping(value = "firewallSave")
	public String firewallSave(SceneFirewall sceneFirewall,String routerId,String environmentId, Model model) {
		logger.debug("dingh==firewallSave=routerId= [{}]", routerId);

		sceneRouterService.firewallSave(sceneFirewall, routerId);

		List<SceneFirewallRule> sceneFirewallRuleList=sceneRouterService.findFirewallRuleList(sceneFirewall.getId());
		model.addAttribute("firewallId", sceneFirewall.getId());
		model.addAttribute("sceneFirewallRuleList", sceneFirewallRuleList);
		model.addAttribute("environmentId", environmentId);
		return "modules/scene/sceneFirewallRuleList";
	}
	@RequiresPermissions("scene:sceneRouter:view")
	@RequestMapping(value = "firewallRuleList")
	public String firewallRuleList(String firewallId,String environmentId, Model model,String type) {
		logger.debug("dingh==firewallSave=routerId=type= [{}] [{}] [{}]", firewallId, environmentId,type);


		String firewallName=sceneRouterService.getFirewallById(firewallId).getFirewallName();
		List<SceneFirewallRule> sceneFirewallRuleList=sceneRouterService.findFirewallRuleList(firewallId);
		model.addAttribute("firewallId", firewallId);
		model.addAttribute("sceneFirewallRuleList", sceneFirewallRuleList);
		model.addAttribute("environmentId", environmentId);
		model.addAttribute("type", type);
		model.addAttribute("firewallName", firewallName);

		return "modules/scene/sceneFirewallRuleList";
	}
	@RequiresPermissions("scene:sceneRouter:view")
	@RequestMapping(value = "getRouterFirewall")
	public String getRouterFirewall(String routerId, Model model) {
		logger.debug("dingh==routerId=[{}]", routerId);
		SceneFirewall sceneFirewall=sceneRouterService.getRouterFirewall(routerId);
		if(sceneFirewall!=null)
		{
			String firewallName=sceneFirewall.getFirewallName();
			List<SceneFirewallRule> sceneFirewallRuleList=sceneRouterService.findFirewallRuleList(sceneFirewall.getId());
			model.addAttribute("sceneFirewallRuleList", sceneFirewallRuleList);
			model.addAttribute("firewallName", firewallName);
		}



		return "modules/scene/sceneFirewallRuleList";
	}

	@RequiresPermissions("scene:sceneRouter:view")
	@RequestMapping(value = "firewallRuleForm")
	public String firewallRuleForm(String firewallId,String environmentId, Model model,String type) {
		logger.debug("type==添加防火墙= [{}]", type);

		logger.debug("dingh==firewallSave=routerId= [{}] [{}]", firewallId, environmentId);
		model.addAttribute("type", type);
		model.addAttribute("firewallId", firewallId);
		model.addAttribute("environmentId", environmentId);
		return "modules/scene/sceneFirewallRuleForm";
	}
	@RequiresPermissions("scene:sceneRouter:edit")
	@RequestMapping(value = "firewallRuleSave")
	public String firewallRuleSave(SceneFirewallRule sceneFirewallRule,String environmentId, Model model,String type) {
		logger.debug("dingh==firewallRuleSave== [{}]", environmentId);
		logger.debug("type==防火墙规则= [{}]", type);

		sceneRouterService.firewallRuleSave(sceneFirewallRule);

//		List<SceneFirewallRule> sceneFirewallRuleList=sceneRouterService.findFirewallRuleList(sceneFirewallRule.getFirewallId());
//		model.addAttribute("firewallId", sceneFirewallRule.getFirewallId());
//		model.addAttribute("sceneFirewallRuleList", sceneFirewallRuleList);
//		model.addAttribute("environmentId", environmentId);
//		return "modules/scene/sceneFirewallRuleList";
		List<SceneFirewall> firewallList=sceneRouterService.findFirewallListByEnvironmentId(environmentId);
		model.addAttribute("type", type);
		model.addAttribute("firewallList", firewallList);
		model.addAttribute("environmentId", environmentId);
		return "modules/scene/sceneFirewallList";
	}
}